package com.beiing.tianshuai.tianshuai.activity;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;

import com.beiing.tianshuai.tianshuai.R;
import com.beiing.tianshuai.tianshuai.adapter.ImageBrowserAdapter;
import com.lzy.ninegrid.preview.HackyViewPager;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by magikliu on 2017/9/18.
 */

public class ShowImageFromWebActivity extends Activity{

    @BindView(R.id.viewPager)
    HackyViewPager viewPager;
    @BindView(R.id.tv_pager)
    TextView tvPager;

    private Context context;
    private ImageBrowserAdapter mAdapter;
    private ArrayList<String> imgUrls;
    private String url;
    private int currentIndex;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.activity_show_image_from_web);
        context = this;
        ButterKnife.bind(this);
        initData();
    }

    private void initData() {
        imgUrls = getIntent().getStringArrayListExtra("imageUrlAll");
        url = getIntent().getStringExtra("imageUrl");
        int position = imgUrls.indexOf(url);
        mAdapter = new ImageBrowserAdapter(this, imgUrls);
        viewPager.setAdapter(mAdapter);
        final int size = imgUrls.size();

        if (size > 1){
            tvPager.setVisibility(View.VISIBLE);
            tvPager.setText((position + 1) + "/" + size);
        } else {
            tvPager.setVisibility(View.GONE);
        }

        viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                currentIndex = position;
                int index = position % size;
                tvPager.setText((index + 1) + "/" + size);
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });

        viewPager.setCurrentItem(position);

    }

}
